//
//  HMEmoticonCollectionViewCell.swift
//  微博
//
//  Created by 程双岐 on 16/10/6.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

class HMEmoticonCollectionViewCell: UICollectionViewCell {
    
    lazy var emoticonbutton: [HMEmoticonButton] = [HMEmoticonButton]()
    
    var emoticonArray: [HMEmoticon]? {
        
        didSet{
            guard let entArray = emoticonArray  else {
                return
            }
            
            for button in emoticonbutton{
                button.isHidden = true
            }
            
            for (i,emoticon) in entArray.enumerated(){
                
                let button = emoticonbutton[i]
                button.emoticon = emoticon
//                button.isHidden = false
//                if emoticon.type == "0" {
//                    button.setImage(UIImage(named: emoticon.path!), for: .normal)
//                    button.setTitle(nil, for: .normal)
//                }else{
//                    button.setTitle((emoticon.code! as NSString).emoji(), for: .normal)
//                    button.setImage(nil, for: .normal)
//                }
//                
            }
            
            
        }
        
    }
    
    var indexPath: IndexPath?{
        didSet{
            messageLabel.text = "当前显示的"
        }
    }
    
    
    fileprivate lazy var messageLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.red, fontSize: 30)
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteButtoAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()
    
    @objc func deleteButtoAction(){
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNotification), object: nil)
    }
    
    private func setupUI(){
        addChildEmoticonButton()
        contentView.addSubview(deleteButton)
////        contentView.addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(self)
//        }
        
    }
    
    func addChildEmoticonButton() {
        for _ in 0..<20{
            let button = HMEmoticonButton()
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            //            button.backgroundColor = RandomColor()
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            contentView.addSubview(button)
            emoticonbutton.append(button)
        }
    }
    @objc private func emoticonButtonAction(btn: HMEmoticonButton) {
        
        print("哈哈")
        
        let emoticon = btn.emoticon!
        
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedEmoticonNotification), object: emoticon)
        let popView = HMEmoticonPopView.emoticonPopView()
        //  设置popview的表情模型
        popView.emotionButton.emoticon = emoticon
        
        //  获取window
        let window = UIApplication.shared.windows.last!
        
        window.addSubview(popView)
        
        
        //  btn在collectionView的区域上转成window的区域
        let convertRect = contentView.convert(btn.frame, to: window)
        
        print(convertRect)
        //  获取这个范围的水平的中心点x
        popView.centerX = convertRect.midX
        popView.y = convertRect.maxY - popView.height
        
        //  延时0.25秒移除
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) {
            popView.removeFromSuperview()
        }
        
        
    }
    

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemWidth = width / 7
        let itemHeight = height / 3
        
        
        for( i, button )in emoticonbutton.enumerated(){
            button.size = CGSize(width: itemWidth, height: itemHeight)
            let colIndex = i % 7
            let rowIndex = i / 7
            
            button.x = CGFloat(colIndex) * itemWidth
            button.y = CGFloat(rowIndex) * itemHeight
        }
        deleteButton.size = CGSize(width: itemWidth, height: itemHeight)
        deleteButton.x = width - itemWidth
        deleteButton.y = height - itemHeight
    }
}
